package com.example.serverjava.service;


import com.example.serverjava.constant.ServerConstant;
import com.example.serverjava.poji.ServerInfo;
import com.example.serverjava.utils.ClassCompareUtil;
import com.example.serverjava.utils.R;
import com.example.serverjava.utils.ServerUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;
@Service
public class ServerServiceImpl {

    /**
     * 获取所有的配置信息
     * @return
     */
    public ServerInfo getServerInfo(){
        ServerInfo serverInfo = new ServerInfo();
        String networkName = ServerUtil.getNetworkCardConfigureName();
        serverInfo.setIp(ServerUtil.getConfigure(ServerConstant.IPADDR,networkName));
        serverInfo.setNetmask(ServerUtil.getConfigure(ServerConstant.NETMASK,networkName));
        serverInfo.setGateway(ServerUtil.getConfigure(ServerConstant.GATEWAY,networkName));
        serverInfo.setDns1(ServerUtil.getConfigure(ServerConstant.DNS1,networkName));
        serverInfo.setDns2(ServerUtil.getConfigure(ServerConstant.DNS2,networkName));
        return serverInfo;
    }

    /**
     * 设置所有的配置信息
     * @param serverInfo
     * @return
     */
    public R setServerInfo(@RequestBody ServerInfo serverInfo){
        ServerInfo requestServerInfo = getServerInfo();
        Map<String, Map<String, Object>> stringMapMap = ClassCompareUtil.compareFields(requestServerInfo, serverInfo);
        if(!ObjectUtils.isEmpty(stringMapMap)){
            if (!ObjectUtils.isEmpty(stringMapMap.get("ip"))){
                String ip = (String) stringMapMap.get("ip").get("newValue");
                ServerUtil.delConfigure(ServerConstant.IPADDR);
                ServerUtil.setConfigure(ip,ServerConstant.IPADDR);
            }
            if (!ObjectUtils.isEmpty(stringMapMap.get("netmask"))){
                String netmask = (String) stringMapMap.get("netmask").get("newValue");
                ServerUtil.delConfigure(ServerConstant.NETMASK);
                ServerUtil.setConfigure(netmask,ServerConstant.NETMASK);
            }
            if (!ObjectUtils.isEmpty(stringMapMap.get("gateway"))){
                String gateway = (String) stringMapMap.get("gateway").get("newValue");
                ServerUtil.delConfigure(ServerConstant.GATEWAY);
                ServerUtil.setConfigure(gateway,ServerConstant.GATEWAY);
            }
            if (!ObjectUtils.isEmpty(stringMapMap.get("dns1"))){
                String dns1 = (String) stringMapMap.get("dns1").get("newValue");
                ServerUtil.delConfigure(ServerConstant.DNS1);
                ServerUtil.setConfigure(dns1,ServerConstant.DNS1);
            }
            if (!ObjectUtils.isEmpty(stringMapMap.get("dns2"))){
                String dns2 = (String) stringMapMap.get("dns2").get("newValue");
                ServerUtil.delConfigure(ServerConstant.DNS2);
                ServerUtil.setConfigure(dns2,ServerConstant.DNS2);
            }
            // 重启网络
            ServerUtil.restartNetwork();
            // 重启服务器
            ServerUtil.reboot();
        }
        return R.ok();
    }
}
